import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import getTransData from "./functions/getTransData";
import beforeTableEdit from "./functions/beforeTableEdit";
import afterTableEdit from "./functions/afterTableEdit";
import admitTransData from "./functions/admitTransData";
import AlertModal from "../../../AlertModal";

const {NCModal, NCButton, NCCheckbox} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class AdmitModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isSynWork: true,
            isFinshPart: true,
            subVisible: false
        };
        this.closeModal = this.closeModal.bind(this);
        this.beSure = this.beSure.bind(this);
        this.initModal = this.initModal.bind(this);
        this.beforeEventHandle = this.beforeEventHandle.bind(this);
        this.afterEventHandle = this.afterEventHandle.bind(this);
        this.confirmTransIn = this.confirmTransIn.bind(this);
        this.closeSubModal = this.closeSubModal.bind(this);
    }

    initModal() {
        getTransData.call(this, this.props.admitPks);
        this.props.editTable.setStatus('PsnReceive', 'edit');
    }

    beSure() {
        const {editTable} = this.props;
        const requireFlag = editTable.checkRequired('PsnReceive', editTable.getAllRows('PsnReceive'));
        if (requireFlag) {
            this.setState({
                isSynWork: true,
                isFinshPart: true,
                subVisible: true
            })
        }
    }

    closeModal() {
        this.props.onClose && this.props.onClose();
    }

    beforeEventHandle(props, moduleId, item, index, value, record) {
        this.oladValue = value.value;
        return beforeTableEdit.call(this, moduleId, item.attrcode, record);
    }

    afterEventHandle(props, moduleId, key, value, changedrows, index, record) {
        afterTableEdit.call(this, moduleId, key, record, this.oladValue, index)
    }

    confirmTransIn() {
        admitTransData.call(this, this.props.admitPks);
    }

    closeSubModal() {
        this.setState({
            subVisible: false
        })
    }

    render() {
        const {visible, editTable} = this.props;
        const {isSynWork, isFinshPart, subVisible} = this.state;
        return (
            <div>
                <NCModal
                    show={visible}
                    size="lg"
                    onHide={this.closeModal}
                    onEntered={this.initModal}
                    backdrop={'static'}
                >
                    <NCModalHeader
                        closeButton={true}
                    >
                        {this.props.json['hi6007-000136']}{/* 国际化处理： 跨组织转入*/}
                    </NCModalHeader>
                    <NCModalBody>
                        {editTable.createEditTable('PsnReceive', {
                            onBeforeEvent: this.beforeEventHandle,
                            onAfterEvent: this.afterEventHandle
                        })}
                    </NCModalBody>
                    <NCModalFooter>
                        <NCButton colors="primary" onClick={this.beSure}>
                            {this.props.json['hi6007-000174']}{/* 国际化处理： 确认*/}
                        </NCButton>
                        <NCButton shape="border" onClick={this.closeModal}>
                            {this.props.json['hi6007-000005']}{/* 国际化处理： 取消*/}
                        </NCButton>
                    </NCModalFooter>
                </NCModal>

                <NCModal
                    show={subVisible}
                    size="sm"
                    onHide={this.closeSubModal}
                >
                    <NCModalHeader
                        closeButton={true}
                    >
                        {this.props.json['hi6007-000137']}{/* 国际化处理： 询问*/}
                    </NCModalHeader>
                    <NCModalBody>
                        <div>
                            {this.props.json['hi6007-000138']}？{/* 国际化处理： 是否确定执行人员调配*/}
                        </div>
                        <div>
                            <NCCheckbox
                                onChange={(value) => this.setState({
                                    isSynWork: value
                                })}
                                checked={isSynWork}>{this.props.json['hi6007-000053']}</NCCheckbox>{/* 国际化处理： 同步履历*/}
                            <NCCheckbox
                                onChange={(value) => this.setState({
                                    isFinshPart: value
                                })}
                                checked={isFinshPart}>{this.props.json['hi6007-000139']}</NCCheckbox>{/* 国际化处理： 结束兼职*/}
                        </div>
                    </NCModalBody>
                    <NCModalFooter>
                        <NCButton colors="primary" onClick={this.confirmTransIn}>
                            {this.props.json['hi6007-000174']}{/* 国际化处理： 确认*/}
                        </NCButton>
                        <NCButton shape="border" onClick={this.closeSubModal}>
                            {this.props.json['hi6007-000005']}{/* 国际化处理： 取消*/}
                        </NCButton>
                    </NCModalFooter>
                </NCModal>

                <AlertModal ref={node => this.alertModalNode = node} json={this.props.json}/>
            </div>
        );
    }
}

export default AdmitModal;
